<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>定义一个常量</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.define" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">define</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">define</span> &mdash; <span class="dc-title">定义一个常量</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.define-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>define</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$case_insensitive</code><span class="initializer"> = false</span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
  在运行时定义一个常量。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.define-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">name</code></dt>

     <dd>

      <p class="para">
       常量名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
      常量的值；在 PHP 5 中，<code class="parameter">value</code> 必须是标量( <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>、
       <span class="type"><a href="language.types.float.html" class="type float">float</a></span>、<span class="type"><a href="language.types.string.html" class="type string">string</a></span>、<span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span>、<strong><code>NULL</code></strong>）在 PHP 7 中还允许是个 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 的值。
      </p>
      <div class="warning"><strong class="warning">Warning</strong>
       <p class="para">
        常量还可以定义为 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> 类型，但并不推荐这样做，因为可能会有不可预知的行为发生。
       </p>
      </div>
     </dd>

    
    
     <dt>
<code class="parameter">case_insensitive</code></dt>

     <dd>

      <p class="para">
       如果设置为 <strong><code>TRUE</code></strong>，该常量则大小写不敏感。默认是大小写敏感的。比如，
       <em>CONSTANT</em> 和 <em>Constant</em> 代表了不同的值。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        大小写不敏感的常量以小写的方式储存。
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.define-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 
 <div class="refsect1 changelog" id="refsect1-function.define-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.0.0</td>
       <td>
        允许 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 的值。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 
 <div class="refsect1 examples" id="refsect1-function.define-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4532">
    <p><strong>Example #1 定义常量</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">"CONSTANT"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;world."</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"Hello&nbsp;world."<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">Constant</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"Constant"&nbsp;并导致&nbsp;Notice<br /><br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"GREETING"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;you."</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">GREETING</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"Hello&nbsp;you."<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">Greeting</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"Hello&nbsp;you."<br /><br />//&nbsp;&nbsp;PHP&nbsp;7&nbsp;起就可以运行了<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'ANIMALS'</span><span style="color: #007700">,&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'dog'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'cat'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bird'<br /></span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">ANIMALS</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"cat"<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.define-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="defined.html" class="function" rel="rdfs-seeAlso">defined()</a> - 检查某个名称的常量是否存在</span></li>
    <li class="member"><span class="function"><a href="constant.html" class="function" rel="rdfs-seeAlso">constant()</a> - 返回一个常量的值</span></li>
    <li class="member"><a href="language.constants.html" class="link">Constants</a>这一节</li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>